﻿using System;
using System.ComponentModel;
using TechMadeira.Data;

namespace TechMadeira.App.UITypeEditors
{
    internal class ClientTypeConverter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType )
        {
            if ( ( sourceType == typeof (string) ) ) return true;
            return base.CanConvertFrom( context, sourceType );
        }
        public override object ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type type )
		{
			if ( ( type == typeof(string) ) && ( value is IClient ) )
			{
			    var client = value as IClient;
			    return client.Name;
            }
            if ((type == typeof (string)) && (value == null))
            {
                return "No client assigned...";
            }
            return base.ConvertTo( context, culture, value, type );
		}

        public override object ConvertFrom( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value )
        {
            //if ( value.GetType() == typeof (string) )
            //    return new MyType() { Name = (string) value };

            return base.ConvertFrom( context, culture, value );
        }
    }
}
